<template>
	<view class="device">
		<view class="head" :style="{height: pHeight + tHeight + 'px'}">
			<view class="xztitle" :style="[{height: pHeight + 'px', top: tHeight + 'px',lineHeight: pHeight + 'px'}]">
				设备安装
				<view class="iconfont icon-dayuhao1" @click="fanhui"></view>
			</view>
		</view>
		<view class="headtop" :style="{marginTop: pHeight + tHeight + 'px'}">
			<image :src="baseUrl + '/wximage/saomiao.png'" class="bj" @click="scanCode"></image>
			<view class="name">扫一扫</view>
		</view>
		<view class="xinxi clearfix">
			<view class="left fl">
				<image :src="baseUrl + '/wximage/hktubiao.png'"></image>
			</view>
			<view class="right fl">
				<view class="title">底座编号</view>
				<view class="bianma">{{bianma}}</view>
			</view>
		</view>
		<view class="footer">
			<view class="submit" @click="puhuo">铺货</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				baseUrl: getApp().globalData.baseUrl,
				headerTop: 0,
				pHeight: 0,
				tHeight: 0,
				bianma: '',
				sn: ''
			}
		},
		onLoad(option) {
			// 头部距离
			let res = uni.getSystemInfoSync() ///微信api方法
			let titleH;
			if (res && res['system']) {
				// 判断是否是安卓操作系统 （标题栏苹果为44px,安卓为48px）
				if (res['system'].indexOf('Android') > 0) {
					titleH = 48
				} else {
					titleH = 46
				}
				this.headerTop = titleH + res['statusBarHeight']
				this.pHeight = titleH
				this.tHeight = res['statusBarHeight']
			}
		},
		methods: {
			puhuo(){
				if(this.bianma==''||this.bianma==null||this.bianma==undefined){
					uni.showToast({
						title: '请扫描二维码再铺货',
						icon: 'none'
					});
					return false;
				}
				uni.redirectTo({
					url: '/subpackageA/device/merchant',
				});
			},
			scanCode() {
				uni.scanCode({
					scanType:['qrCode'],
					success: (res) => {
						if(res.result){
							let url = res.result;
							let qrcodeurl = getApp().globalData.baseUrl;
								//给设备的所属
								// if(url.substring(0, (qrcodeurl.length))==qrcodeurl){
								if(url.indexOf('/scan') > -1){
									var arrUrl = url.split('qrcodenumber=');
									var qrcode = arrUrl[1];
									var formData = {
										'agent_id': uni.getStorageSync('agent_id'),
										'qrcode': qrcode
									}
									//请求接口绑定该商户
									this.request({
										url: '/wxdevice/deviceinstall',
										data: formData,
										method: "POST"
									}).then(res => {
										if (res.data.code == 200) {
											
											this.bianma = res.data.data;
											uni.setStorageSync('puhuosn',this.bianma);
											
										} else {
											uni.showToast({
												title: res.data.message,
												icon: 'none'
											});
										}
									})
								}
						}
					},
					
					fail: (res) => {
						// console.log("未识别到二维码");
						uni.showToast({
							title: "未识别到二维码",
							icon: 'none'
						});	
					}
				})
			},
			fanhui() {
				uni.navigateBack();
			}
		}
	}
</script>

<style lang="scss" scoped>
	.device {
		width: 100%;
		margin: 0 auto;
		height: 100vh;
		background: #f9f9ff;
		overflow-y: scroll;

		.head {
			width: 100%;
			position: fixed;
			top: 0;
			left: 0;
			z-index: 99;
			background: linear-gradient(90deg, #f9f9ff, #d4e8ff);

			.xztitle {
				position: fixed;
				left: 0%;
				top: 0;
				width: 100%;
				text-align: left;
				color: #333333;
				font-size: 32rpx;
				padding-left: 74rpx;
				font-weight: 700;

				text {
					font-size: 24rpx;
				}

				.iconfont {
					position: absolute;
					top: 0;
					left: 30rpx;
					font-size: 44rpx;
				}
			}
		}

		.headtop {
			width: 100%;
			background: linear-gradient(90deg, #f9f9ff, #d4e8ff);
			height: 300rpx;
			text-align: center;
			
			.bj {
				width: 100rpx;
				height: 100rpx;
				margin-top: 108rpx;
			}
			
			.name {
				font-size: 28rpx;
				color: #333333;
				font-weight: 700;
				margin-top: 6rpx;
			}
		}

		.xinxi {
			width: 94%;
			margin: 0 auto;
			background: #FFFFFF;
			border-radius: 20rpx;
			margin-top: 20rpx;
			padding: 20rpx;
			box-sizing: border-box;
			box-shadow:rgba(223, 230, 255, 0.67) 0px 0px 6px;
			
			.left{
				
				image{
					width: 40rpx;
					height: 40rpx;
				}
			}
			
			.right{
				
				.title{
					color: #333333;
					margin-bottom: 20rpx;
				}
				
				.bianma{
					font-size: 24rpx;
					color: #666666;
					font-weight: 700;
					margin-bottom: 30rpx;
				}
			}
		}

		.footer {
			width: 80%;
			margin: 0 auto 40rpx;
			margin-top: 360rpx;
			height: 72rpx;
			text-align: center;
			background: linear-gradient(90deg, #65bdfc, #398ef8);
			border-radius: 36rpx;
			border-top-left-radius: 0;
			border-bottom-left-radius: 42rpx;
			
			.submit {
				height: 72rpx;
				line-height: 72rpx;
				color: #FFFFFF;
				font-size: 32rpx;
			}
		}
	}
</style>